/**
 * Created : Feb 26, 2012
 *
 * @author pquiring
 */

import java.io.*;
import java.awt.event.*;
import javax.swing.*;

import javaforce.*;
import javaforce.jbus.*;

public class MainPanel extends javax.swing.JPanel implements ActionListener {

  /**
   * Creates new form Welcome
   */
  public MainPanel() {
    initComponents();
    String jid = System.getenv("JID");
    if (jid == null) {
      run.setEnabled(false);
    } else {
      String pack = "org.jflinux.jfwelcome." + jid;
      jbusClient = new JBusClient(pack, new JBusMethods());
      jbusClient.start();
      jbusClient.call("org.jflinux.jdesktop." + jid, "getWelcome", "\"" + pack + "\"");
    }
    addButtons();
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    run = new javax.swing.JCheckBox();
    close = new javax.swing.JButton();
    buttons = new javax.swing.JPanel();

    jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/logo.png"))); // NOI18N
    jLabel1.setToolTipText("");

    run.setSelected(true);
    run.setText("Show Welcome Screen on next logon");
    run.addItemListener(new java.awt.event.ItemListener() {
      public void itemStateChanged(java.awt.event.ItemEvent evt) {
        runItemStateChanged(evt);
      }
    });

    close.setText("Close");
    close.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        closeActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addComponent(run)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(close))
          .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(buttons, javax.swing.GroupLayout.PREFERRED_SIZE, 594, javax.swing.GroupLayout.PREFERRED_SIZE)
              .addComponent(jLabel1))
            .addGap(0, 0, Short.MAX_VALUE)))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jLabel1)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(buttons, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(run, javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(close, javax.swing.GroupLayout.Alignment.TRAILING))
        .addContainerGap())
    );
  }// </editor-fold>//GEN-END:initComponents

  private void closeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeActionPerformed
    System.exit(0);
  }//GEN-LAST:event_closeActionPerformed

  private void runItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_runItemStateChanged
    jbusClient.call("org.jflinux.jdesktop." + System.getenv("JID"), "setWelcome", "\"" + run.isSelected() + "\"");
  }//GEN-LAST:event_runItemStateChanged

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JPanel buttons;
  private javax.swing.JButton close;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JCheckBox run;
  // End of variables declaration//GEN-END:variables

  private JBusClient jbusClient;

  public void actionPerformed(ActionEvent ae) {
    String action = ae.getActionCommand();
    if (action == null) return;
    try {
      Runtime.getRuntime().exec(action);
    } catch (Exception e) {
      System.out.println("Exception:" + e);
    }
  }

  public class JBusMethods {
    public void setWelcome(String state) {
      run.setSelected(state.equals("true"));
    }
  }

  private void addButton(String bin, String txt, String icon) {
    JFImage iconimg = new JFImage();
    iconimg.loadPNG(getClass().getClassLoader().getResourceAsStream(icon));
    JButton button = new JButton(txt, iconimg);
    button.setHorizontalTextPosition(SwingConstants.CENTER);
    button.setVerticalTextPosition(SwingConstants.BOTTOM);
    button.setActionCommand(bin);
    button.addActionListener(this);
    buttons.add(button);
  }

  private void addButtons() {
    //jinstall
    if (new File("/usr/bin/jinstall").exists()) {
      addButton("/usr/bin/jinstall", "Installer", "jinstall.png");
    }
    //japps
    if (new File("/usr/bin/japps").exists()) {
      addButton("/usr/bin/japps", "Add/Remove Programs", "zip.png");
    }
    //jconfig
    if (new File("/usr/bin/jconfig").exists()) {
      addButton("/usr/bin/jconfig", "Configure System", "jconfig.png");
    }
    //jrepo
    if (new File("/usr/bin/jrepo").exists()) {
      addButton("/usr/bin/jrepo", "Select Repository", "zip.png");
    }
    //jhelp
    if (new File("/usr/bin/jhelp").exists()) {
      addButton("/usr/bin/jhelp", "Help System", "jhelp.png");
    }
  }
}
